package Practice6_Internet.P2_TCP;

import Utils.web.TcpUtils;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class PracticeTest02New {
    @Test
    public void client() throws IOException {
        try (Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090)) {
            TcpUtils.clientSendFileToServer("./src/test/resources/img104.jpg",socket);
            // 关闭socket输出流
            socket.shutdownOutput();

            // 接收来自服务器端的数据，并显示到控制台
            System.out.println(TcpUtils.clientAcceptMessageFromServer(socket));
        }
    }

    @Test
    public void server() throws IOException {
        try (ServerSocket serverSocket = new ServerSocket(9090);
             Socket socket = serverSocket.accept()) {
            TcpUtils.serverAcceptFileFromClient(socket,"./src/test/resources/img104_New.jpg");
            System.out.println("图片传输完成");

            // 服务器端给予客户端反馈
            TcpUtils.serverSendMessageToClient("文件已收到！",socket);
        }
    }
}
